﻿using System;
using System.Text;
using System.Collections.Generic;
using TrueSync;

public enum InputActionDefEnum
{
    NO_ACTION = 0,
    A = 1,
    B = 2,
    S = 4,
    T = 8,
    AB = 3,
    ST = 12,
}

public class LogicInputComponent : LogicComponent
{
    LogicEntity mLEntity;
    LogicMoveComponent mMoveComponent;
    public Action<string> OnInputCallback;

    const int MAX_DIRECTION_COMBINATION_LEN = 10;
    const int MAX_DIRECTION_COMBINATION_FRAME_INTERVAL = 2;
    List<int> mInputDirectionCombinations;
    int LastInputDirection;
    int mFrames;
    int mLastRecvDirectionFrameNo;

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;
        mInputDirectionCombinations = new List<int>(MAX_DIRECTION_COMBINATION_LEN);
        mFrames = 0;
        mLastRecvDirectionFrameNo = 0;
        LastInputDirection = 5;//一开始等于什么都没按下
    }

    public void Start()
    {
        mMoveComponent = mLEntity.GetLogicComponent<LogicMoveComponent>();
    }

    public void DoUpdate(FP deltaTime)
    {
        mFrames++;
        
        //如果时间超了那就把之前的输入清除
        if (mFrames - mLastRecvDirectionFrameNo > MAX_DIRECTION_COMBINATION_FRAME_INTERVAL)
        {
            mInputDirectionCombinations.Clear();
        }
    }

    public void Destroy()
    {
        mMoveComponent = null;
        mLEntity = null;
    }

    public void HandleInput(int operation)
    {
        int inputDirection = DirectionTranslate(operation);
        mLEntity.GetLogicComponent<LogicStateMachineComponent>().CurrentState.OnInputDirection(inputDirection); 
        if(LastInputDirection != 5 //之前的输入不是抬起
            && inputDirection != LastInputDirection)//跟之前有变化
        {
            int currentFrameNo = mFrames + 1;
            if (mInputDirectionCombinations.Count > MAX_DIRECTION_COMBINATION_LEN)
                mInputDirectionCombinations.RemoveAt(0);
            mInputDirectionCombinations.Add(LastInputDirection);
            mLastRecvDirectionFrameNo = currentFrameNo;
        }
        LastInputDirection = inputDirection;
        //是不是有右侧按键
        InputActionDefEnum inputAction = (InputActionDefEnum)ActionTranslate(operation);
        if(inputAction != InputActionDefEnum.NO_ACTION)
        {
            StringBuilder actionString = new StringBuilder();
            for(int i = 0; i < mInputDirectionCombinations.Count; i++)
            {
                actionString.Append(mInputDirectionCombinations[i].ToString());
            }
            //actionString.Append(LastInputDirection.ToString());
            actionString.Append(inputAction.ToString());
            if (OnInputCallback != null)
                OnInputCallback(actionString.ToString());
        }
    }

    int DirectionTranslate(int inputvalue)
    {
        inputvalue = inputvalue & 15;
        int operation;
        switch (inputvalue)
        {
            case 1:
                operation = mMoveComponent.FaceDirection == 1 ? 4 : 6;
                break;
            case 2:
                operation = 2;
                break;
            case 4:
                operation = mMoveComponent.FaceDirection == 1 ? 6 : 4;
                break;
            case 8:
                operation = 8;
                break;
            case 3:
                operation = mMoveComponent.FaceDirection == 1 ? 1 : 3;
                break;
            case 6:
                operation = mMoveComponent.FaceDirection == 1 ? 3 :1;
                break;
            case 9:
                operation = mMoveComponent.FaceDirection == 1 ? 7 : 9;
                break;
            case 12:
                operation = mMoveComponent.FaceDirection == 1 ? 9 : 7;
                break;
            default:
                operation = 5;
                break;
        }
        return operation;
    }

    int ActionTranslate(int input)
    {
        int directionBits = input & 15;
        return (input - directionBits) >> 4;
    }
}
